/**
 * TmPackSerialServiceImpl.java
 * Created at 2017-2-17
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.dao.ITmPackSerialDao;
import com.broadtext.rms.basic.model.TmPackSerial;
import com.broadtext.rms.basic.service.ITmPackSerialService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: TmPackSerialServiceImpl</p>
 * <p>Description: 电池id服务层实现类</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-2-17</p>
 */
@Service("tmPackSerialService")
public class TmPackSerialServiceImpl extends EntityServiceImpl<TmPackSerial> implements ITmPackSerialService {
    
    @Autowired
    @Qualifier("tmPackSerialDao")
    public void setEntityDao(EntityDao<TmPackSerial> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * 注入dao
     */
    @Autowired
    @Qualifier("tmPackSerialDao")
    private ITmPackSerialDao tmPackSerialDao;
    
    /**
     * 
     * <p>
     * Description: 根据id删除
     * </p>
     * @param delIdArray 待删除主键id数组
     * @param currentUser 当前登录人
     * @param request 请求
     */
    public void delete(String[] delIdArray, String currentUser) throws DaoException {
        for (String id : delIdArray) {
            TmPackSerial t;
            t = new TmPackSerial();
            t.setPkId(id);
            t.setUpdateDate(new Date());
            t.setUpdateUser(currentUser);
            t.setDelFlag("1");
            this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmPackSerial.deleteRecord", t);
        }
    }
    
    /**
     * 
     * <p>Description: 查询电池使用情况</p>
     * @return 三个状态使用数量
     * @throws DaoException 异常
     */
    public Map<String, Object> getStatusCount() throws DaoException{
        Map<String, Object> map;
        map  = new HashMap<String, Object>();
        //状态 0:未使用，1：已使用，2：已报废
        long  unUse=  this.tmPackSerialDao.getStatusCount("com.broadtext.rms.basic.model.TmPackSerial.getStatusCount", "0");
        map.put("unUse", unUse);
        long  used=  this.tmPackSerialDao.getStatusCount("com.broadtext.rms.basic.model.TmPackSerial.getStatusCount", "1");
        map.put("used", used);
        long  useless=  this.tmPackSerialDao.getStatusCount("com.broadtext.rms.basic.model.TmPackSerial.getStatusCount", "2");
        map.put("useless", useless);
        return map;
    }
    
    /**
     * 
     * <p>Description: 生成指定数量电池ID</p>
     * @param packQuantity 数量
     * @param currentUser 当前用户
     * @throws DaoException 
     */
    public boolean createPack(long packQuantity, String currentUser) throws DaoException {
        //日期格式化
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        //数字格式化
        DecimalFormat f = new DecimalFormat("0000");
        //查询条件 QDLSYYYYMMDD
        String queryParam = "QDLS";
        queryParam = queryParam + formatter.format(new Date());
        //获取电池list
        List<TmPackSerial> num;
        num = this.tmPackSerialDao.getMaxId("com.broadtext.rms.basic.model.TmPackSerial.getPackIdList", queryParam);
        List<TmPackSerial> newPackList;
        newPackList = new ArrayList<TmPackSerial>();
        if(num.isEmpty()){
            //当天没有生成电池id 即第一次生成电池id
            for (int i = 0; i < packQuantity; i++) {
                String serialNo;
                serialNo = queryParam + f.format(i + 1);
                TmPackSerial t;
                t = new TmPackSerial();
                t.setPkId(UUIDUtils.getUUID());
                t.setDelFlag("0");
                t.setUpdateDate(new Date());
                t.setUpdateUser(currentUser);
                t.setCreateDate(new Date());
                t.setCreateUser(currentUser);
                t.setSerialNo(serialNo);
                t.setStatus("0");
                newPackList.add(t);
            }
        } else {
            //maxId
            int k = Integer.parseInt((num.get(0).getSerialNo()));
            for (int i = 0; i < packQuantity; i++) {
                String serialNo;
                serialNo = queryParam + f.format(k + i + 1);
                TmPackSerial t;
                t = new TmPackSerial();
                t.setPkId(UUIDUtils.getUUID());
                t.setDelFlag("0");
                t.setUpdateDate(new Date());
                t.setUpdateUser(currentUser);
                t.setCreateDate(new Date());
                t.setCreateUser(currentUser);
                t.setSerialNo(serialNo);
                t.setStatus("0");
                newPackList.add(t);
            }
        }
        //批量插入
        this.tmPackSerialDao.insertBatch("com.broadtext.rms.basic.model.TmPackSerial.insertBatch", newPackList);
        return true;
    }
    
    /**
     * 
     * <p>Description: 验证数值是否过大</p>
     * @param packNum 数量
     * @return 布尔值
     * @throws DaoException 异常
     */
    public String checkData(long packNum) throws DaoException {
        String result = "true";
        //日期格式化
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        //查询条件 QDLSYYYYMMDD
        String queryParam = "QDLS";
        queryParam = queryParam + formatter.format(new Date());
        //获取电池list
        List<TmPackSerial> num;
        num = this.tmPackSerialDao.getMaxId("com.broadtext.rms.basic.model.TmPackSerial.getPackIdList", queryParam);
        if(num.isEmpty()){
            if(packNum > 9999){
                result = "false";
            }
        } else {
            int k = Integer.parseInt((num.get(0).getSerialNo()));
            if(packNum > (9999-k)){
                result = "false";
            }
        }
        return result;
    }
    
    /**
     * 
     * <p>Description: 检查待删除电池状态是否合法</p>
     * @param ids id数组
     * @return 检查结果
     * @throws DaoException 异常
     */
    public Object checkPackStatus(String[] ids) throws DaoException {
        return this.tmPackSerialDao.checkPackStatus(ids);
    }
    
    /**
     * 
     * <p>Description: 电池packCode下拉框分页查询</p>
     * @param params 查询条件
     * @param pageBounds 分页
     * @return 结果集
     * @throws DaoException 异常
     */
	public EntityPageBean<TmPackSerial> getPackCodeOptions(Map<String, Object> params,
			PageBounds pageBounds) throws DaoException {
		return this.entityDao.findEntityPageByQuery("com.broadtext.rms.basic.model.TmPackSerial.getPackCodeOptions", params, pageBounds);
	}
	
	/**
     * 
     * <p>Description: 电池packCode下拉框分页查询</p>
     * @param params 查询条件
     * @param pageBounds 分页
     * @return 结果集
     * @throws DaoException 异常
     */
	@Override
    public EntityPageBean<Object> getPackCodeOptions2(Map<String, Object> params,
            PageBounds pageBounds) throws DaoException {
        return this.entityDao.findObjectEntityPage("com.broadtext.rms.basic.model.TmPackSerial.getPackCodeOptions2", params, pageBounds);
    }

    /** (non-Javadoc)
     * <p>Title: customID</p>
     * <p>Description: </p>
     * @param t
     * @return
     * @throws DaoException
     * @see com.broadtext.rms.basic.service.ITmPackSerialService#customID(com.broadtext.rms.basic.model.TmPackSerial)
     */
    @Override
    public Map<String, Object> customID(TmPackSerial t) throws DaoException {
        Map<String, Object> map = new HashMap<String, Object>();
        long num = this.tmPackSerialDao.checkRepeatPackCode(t.getSerialNo());
        if(num > 0){
            map.put("msg", "REPEAT");
        }else {
            boolean f = this.entityDao.insertRecord("com.broadtext.rms.basic.model.TmPackSerial.insertRecord", t);
            if(f){
                map.put("msg", "SUCCESS");
            }else {
                map.put("msg", "FAIL");
            }
        }
        return map;
    }
	
	
}
